package cn.itcast.mq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class CommonConfig implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // RabbitTemplate - return 是个全局的回调
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        // 添加回调
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                log.error("路由消息失败: message:{}, replyCode:{}, replyText: {}, exchange:{}, routingKey:{} ",
                        message, replyCode, replyText, exchange, routingKey);
                // TODO 后续就可以补发了
            }
        });
    }
}
